package com.hanagent.controller;
 
import com.hanagent.model.Agents;
import com.hanagent.model.Goods;
import com.hanagent.utils.ConstantUtils;
import com.hanagent.utils.DateUtils;
import com.hanagent.utils.ResponseUtils;
import com.hanagent.utils.StrUtils;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

public class GoodController extends Controller{
	
	public void getList() {
		int pageNumber = getParaToInt("pageNumber")<1?1:getParaToInt("pageNumber");
		int pageSize = getParaToInt("pageSize")<1?1:getParaToInt("pageSize");
		String startCreateTime = getPara("startCreateTime");
		if (StrUtils.isEmpty(startCreateTime)) {
			startCreateTime = "1917-12-05T16:00:00.000Z";
		} 
		String endCreateTime = getPara("endCreateTime");
		if (StrUtils.isEmpty(endCreateTime)) {
			endCreateTime = "3017-12-05T16:00:00.000Z";
		}  
		String startUpdateTime = getPara("startUpdateTime");
		if (StrUtils.isEmpty(startUpdateTime)) {
			startUpdateTime = "1917-12-05T16:00:00.000Z";
		} 
		String endUpdateTime = getPara("endUpdateTime");
		if (StrUtils.isEmpty(endUpdateTime)) {
			endUpdateTime = "3017-12-05T16:00:00.000Z";
		} 
		Page<Goods> list = Goods.dao.paginate(pageNumber, pageSize, "select * ", "from Goods where createdAt > ? and "
				+ "createdAt < ? and updatedAt > ? and updatedAt < ? order by updatedAt desc", startCreateTime, endCreateTime, startUpdateTime, endUpdateTime);
        renderText(ResponseUtils.sendResult(1, list, "获取数据成功"));
    }
	
	/**  
	 * 添加
     */  
    public void addGoods() {  
    	Goods goods = getModel(Goods.class, "");//别名  Jfinal 必杀技快速开发全靠它  
    	Agents loginAgents = getAttr("loginAgents"); //登陆的代理商
		if (loginAgents.getAgentLevel()!=ConstantUtils.LevelSuper) {
			renderText(ResponseUtils.sendResult(0, "", "只有公司管理员才可以添加商品"));
			return;
		}
        boolean flag = goods.save();  
        if (flag) {  
        	renderText(ResponseUtils.sendResult(1, "", "添加成功"));  
        }else {  
        	renderText(ResponseUtils.sendResult(0, "", "添加失败"));  
        }  
    }

    /** 
     * 修改 
     */   
    public void updateGoods() {  
    	Goods goods = getModel(Goods.class, "");//别名  Jfinal 必杀技快速开发全靠它  
    	Agents loginAgents = getAttr("loginAgents"); //登陆的代理商
		if (loginAgents.getAgentLevel()!=ConstantUtils.LevelSuper) {
			renderText(ResponseUtils.sendResult(0, "", "只有公司管理员才可以更新商品"));
			return;
		}
		goods.setUpdatedAt(DateUtils.getNowDateWith8());
        boolean flag = goods.update();
        if (flag) {  
        	renderText(ResponseUtils.sendResult(1, "", "修改成功"));  
        }else {  
        	renderText(ResponseUtils.sendResult(0, "", "修改失败"));  
        }   
    }  
      
    /** 
     * 删除单个 
     */  
    public void delGoodsById() {  
        Integer id = getParaToInt("goodId");  
        Agents loginAgents = getAttr("loginAgents"); //登陆的代理商
		if (loginAgents.getAgentLevel()!=ConstantUtils.LevelSuper) {
			renderText(ResponseUtils.sendResult(0, "", "只有公司管理员才可以删除商品"));
			return;
		}
        boolean flag = Goods.dao.deleteById(id);  
        if (flag) {  
        	renderText(ResponseUtils.sendResult(1, "", "删除成功"));  
        }else {  
        	renderText(ResponseUtils.sendResult(0, "", "删除失败"));  
        }    
    }  
}
